package com.xiaomazi.cms.controller;

import com.xiaomazi.cms.entity.Banner;
import com.xiaomazi.cms.service.BannerService;
import com.xiaomazi.common.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @program: llxy-parent
 * @description: Banner后台控制器
 * @author: 小马子
 * @create: 2023-03-07 11:04
 **/
@RestController
@RequestMapping("/educms/banner")
public class BannerController {

    @Autowired
    private BannerService bannerService;

    @GetMapping("/getAllBannerList/{current}/{size}")
    public R getAllBannerList(@PathVariable Integer current,
                              @PathVariable Integer size){
        return bannerService.getAllBannerList(current,size);
    }

    @DeleteMapping("/deleteBannerById/{id}")
    public R deleteBannerById(@PathVariable String id){
        return bannerService.removeById(id) ? R.ok() : R.error().message("删除Banner出错啦");
    }

    @PostMapping("/saveBanner")
    public R saveBanner(@RequestBody Banner banner){
        return bannerService.save(banner) ? R.ok():R.error().message("新增Banner出错啦");
    }

    @PostMapping("/updateBanner")
    public R updateBanner(@RequestBody Banner banner){
        return bannerService.updateById(banner) ? R.ok():R.error().message("修改Banner出错啦");
    }

    @GetMapping("/getBannerById/{id}")
    public R getBannerById(@PathVariable String id){
        return R.ok().data("bannerInfo",bannerService.getById(id));
    }

}
